import java.sql.*;

public class TestMysqlJdbc {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/myedulearn?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true","root","root");
            /*String sql="select * from user where id=?";
            Integer id=1;
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1,id);*/
            String sql="select * from user";
            preparedStatement = connection.prepareStatement(sql);
            boolean execute = preparedStatement.execute();
            if(execute){
                ResultSet resultSet = preparedStatement.getResultSet();
                while (resultSet.next()){
                    Integer id1 = (Integer) resultSet.getObject("id");
                    String username = (String)resultSet.getObject("username");
                    System.out.println("查询到结果：{id:"+id1+",username:"+username+"}");
                }

            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(preparedStatement != null)preparedStatement.close();
                if(connection != null) connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
